/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientman;

/**
 *
 * @author wallis
 */
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class w_main extends javax.swing.JFrame {

    /**
     * Creates new form w_main
     */
    Socket sk;
    BufferedWriter out;
    BufferedReader in;

    public w_main() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jT_ip = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jT_chat = new javax.swing.JTextArea();
        jL_status = new javax.swing.JLabel();
        jB_send = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jT_message = new javax.swing.JTextArea();
        jT_port = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);

        jT_ip.setText("192.168.1.5");

        jLabel1.setText("IP:");

        jButton1.setText("连接");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jT_chat.setColumns(20);
        jT_chat.setRows(5);
        jScrollPane1.setViewportView(jT_chat);

        jL_status.setText("状态");

        jB_send.setText("Send");
        jB_send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_sendActionPerformed(evt);
            }
        });

        jT_message.setColumns(20);
        jT_message.setRows(5);
        jScrollPane2.setViewportView(jT_message);

        jT_port.setText("7000");
        jT_port.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jB_send))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jT_ip, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jT_port, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL_status, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jT_ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1)
                    .addComponent(jL_status)
                    .addComponent(jT_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jB_send)
                .addGap(6, 6, 6))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        ConnectToServer aConnectToServer = new ConnectToServer();
        Thread aThread = new Thread(aConnectToServer);
        aThread.start();
   }//GEN-LAST:event_jButton1ActionPerformed

    private void jB_sendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_sendActionPerformed
        try {
            // TODO add your handling code here:
            out.write("Client:"+"\r\n"+jT_message.getText() +"\r\n");
            out.flush();
            jT_chat.setText(jT_chat.getText() + "Client:"+"\r\n"+jT_message.getText() + "\r\n");
            jT_message.setText("");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Client_SenddMessage:"+ex.getMessage());;
        }
       
    }//GEN-LAST:event_jB_sendActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_send;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jL_status;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jT_chat;
    private javax.swing.JTextField jT_ip;
    private javax.swing.JTextArea jT_message;
    private javax.swing.JTextField jT_port;
    // End of variables declaration//GEN-END:variables

    private void ReadMessage() {
        String s_msg = null;
        do {
            try {
                s_msg =in.readLine();
                jT_chat.setText(jT_chat.getText() + s_msg+"\r\n");
            } catch (IOException ie) {
                JOptionPane.showMessageDialog(this, "Client_ReadMessage:"+ie.getMessage());
                break;
            }
        } while (true);
    }

    private class ConnectToServer implements Runnable {
        @Override
        public void run() {
            try {
                    sk = new Socket(InetAddress.getByName(jT_ip.getText()), Integer.valueOf(jT_port.getText()));
                    out = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream(),"GBK"));
                    in = new BufferedReader(new InputStreamReader(sk.getInputStream(),"GBK"));
                    Thread aThread1 = new Thread() {
                        public void run() {
                            ReadMessage();
                        }
                    };
                    aThread1.start();

            } catch (IOException ie) {
                jT_chat.setText(jT_chat.getText() + "\r\nConnectToServer:" + ie.toString() + "\r\n");
            }
        }
    }
}
